<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2019/4/30
  Time: 10:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="../common.jsp"%>
<html>
<head>
    <title>任务列表</title>
</head>
<body>
<table id="taskGrid" class="easyui-datagrid" title="流程列表" style="width:100%;height:100%" toolbar="#tb">
    <thead>
    <tr>
        <th field="id" width="100" >任务编码</th>
        <th field="name" width="100">任务名称</th>
        <th field="createTime" width="150" align="rigth">创建时间</th>
        <th field="assignee" width="100" align="center">委派人</th>
        <th field="userName" width="100" align="center">申请人</th>
        <th field="applyName" width="100" align="center">申请名称</th>
        <th field="processInstanceId" width="100" align="right">流程实例</th>
    </tr>
    </thead>
</table>

<div id="tb">
    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="handleTask()">办理任务</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="showFlowInfo()">查看流程详情</a>
</div>

<div id="flow_show" class="easyui-dialog" title="新增业务请求" data-options="iconCls:'icon-save'" style="border: 0; width: 500px;">
    <table id="infoGrid" class="easyui-datagrid" title="流程详情" style="width:1000px;height:500px">
        <thead>
        <tr>
            <th field="taskId" width="100">任务编码</th>
            <th field="applyName" width="100">任务名称</th>
            <th field="applyContent" width="150" align="rigth">任务内容</th>
            <th field="handleName" width="100" align="center">审批人</th>
            <th field="opinion" width="100" align="center">审批意见</th>
            <th field="flag" width="100" align="right">审批状态</th>
            <th field="createTime" width="100" align="center">审批时间</th>
        </tr>
        </thead>
    </table>
</div>
<div id="taskhandle_dialog" class="easyui-dialog" title="业务审批受理" data-options="iconCls:'icon-save'" style="border: 0; width: 500px;">
    <form id="mainForm" method="post">
        <table cellpadding="5">
            <input type="text" name="taskId" id="taskId" hidden="true"/>
            <tr>
                <td>申请业务名称:</td>
                <td><input class="easyui-textbox" type="text" name="applyName"  data-options="required:true"></input></td>
            </tr>
            <tr>
                <td>申请业务内容:</td>
                <td><input class="easyui-textbox" type="text" name="applyContent"  data-options="required:true"></input></td>
            </tr>

            <tr>
                <td>受理意见:</td>
                <td><input class="easyui-textbox" type="text" name="opinion"  data-options="required:true"></input></td>
            </tr>

            <tr id="yy" style="display: none">
                <td>
                    <input class="easyui-radio" type="radio" name="type"  value="1"  checked="checked"/>销售业务
                    <input class="easyui-radio" type="radio" name="type" value="2"/>运营业务
                </td>
            </tr>
        </table>

    </form>
    <div style="text-align: center; padding: 5px">
        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="passRequest('1')">审批通过</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="passRequest('0')">驳回请求</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="endTaskReq()">终止任务</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="closeForm()">取消</a>
    </div>

</div>

<script type="application/javascript">
    $(function () {
        $("#taskhandle_dialog").dialog('close');
        $("#flow_show").dialog('close');
        $("#taskGrid").datagrid({
            url:ctx+"/task/getTaskList",
            pagination:true,
            pageSize:20,
            pageList:[20,50,100],
            singleSelect:true,
            rownumbers:true,
            onBeforeLoad : function(param) {
                $.messager.progress({
                    text : '数据加载中....'
                });
            },
            onLoadSuccess : function(data) {
                $.messager.progress('close');
            },
            onLoadError : function() {
                $.messager.progress('close');
            }
        });
    });
function handleTask(){
    var row = $("#taskGrid").datagrid("getSelected");
    if(row){
        $("#taskhandle_dialog").dialog('open');
        $("#taskId").val(row.id);
        $.ajax({
            url: ctx + '/task/showHanldeTaskById',
            data: {
                taskId: row.id
            },
            type: "GET",
            success: function (data) {
                if (data.status) {
                    // 重新加载
                    var obj = data.respObj;
                    $('#mainForm').form('load', {
                        applyName:obj.applyName,
                        applyContent:obj.applyContent
                    });
                    if(obj.remark=="userTask1"){
                       $("#yy").show();
                    }
                } else {
                    $.messager.alert("操作提示", data.respMsg, "error");
                }
            },
            error: function () {
                $.messager.alert("操作提示", "操作失败！", "error");
            }
        });
    }else{
        $.messager.alert("操作提示", "请选择一项代办处理！", "error");
    }
}

/**终止任务*/
function endTaskReq(){
    var row = $("#taskGrid").datagrid("getSelected");
    if(row){
        $.messager.confirm("提示","您确认终止当前任务吗？",function(data){
            if(data){
                $.ajax({
                    url: ctx + '/endTask/dead',
                    data: {
                        taskId: row.id
                    },
                    type: "GET",
                    success: function (data) {
                        if (data.status == true) {
                            $("#infoGrid").datagrid('reload');
                            $.messager.alert("操作提示", data.respMsg, "info");
                        } else {
                            $.messager.alert("操作提示", data.respMsg, "error");
                        }
                    },
                    error: function () {
                        $.messager.alert("操作提示", "操作失败！", "error");
                    }
                });
            }
        });
    }else{
        $.messager.alert("操作提示", "请选择一项代办处理！", "error");
    }
}


function passRequest(data){
    var flag=false;
    if(data=='1'){
        flag=true;
    }
    var formJson = $('#mainForm').find('input').not('[value=""]').serializeJSON();
    // alert(JSON.stringify(formJson))
    formJson.flag = flag;
    $.ajax({
        url: ctx + '/task/handleTask',
        type: "post",
        data:formJson,
        success: function (data) {
            if (data.status == true) {
                // 重新加载
                $("#taskhandle_dialog").dialog('close');
                $("#infoGrid").datagrid('reload');
                $.messager.alert("操作提示", data.respMsg, "info");

            } else {
                $.messager.alert("操作提示", data.respMsg, "error");
            }
        },
        error: function () {
            $.messager.alert("操作提示", "操作失败！", "error");
        }
    });
}



function closeForm(){
    $("#taskhandle_dialog").dialog('close');
}


    function showFlowInfo(){
        var row = $("#flowGrid").datagrid("getSelected");
        if(row){
            $('#flow_show').dialog('open');
            $("#infoGrid").datagrid({
                url:ctx+"/flow/showFlowProcess?processInstanceId="+row.processInstanceId,
                pagination:true,
                pageSize:20,
                pageList:[20,50,100],
                singleSelect:true,
                rownumbers:true,
                onBeforeLoad : function(param) {
                    $.messager.progress({
                        text : '数据加载中....'
                    });
                },
                onLoadSuccess : function(data) {
                    $.messager.progress('close');
                },
                onLoadError : function() {
                    $.messager.progress('close');
                }
            });
        }
    }
</script>
</body>
</html>
